package jdk.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//演员的代理类
public class ActorProxy implements InvocationHandler {

    private Object orientObject; //被代理对象

    //返回一个代理对象。
    public Object newProxyInstance(Object targetObject){
        this.orientObject=targetObject;
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(),this);
    }

    //演戏之前要做的事情
    private void beforeAct(){
        System.out.println("洽谈档期..");
        System.out.println("洽谈片酬..");
        System.out.println("洽谈广告合作..");
    }

    //演戏之后要做的事情
    private void afterAct(){
        System.out.println("洽谈首映仪式..");
        System.out.println("洽谈颁奖典礼..");
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object ret = null;
        this.beforeAct();
        ret=method.invoke(orientObject, args); //调用真正成龙演戏的方法
        this.afterAct();

        return ret;
    }
}
